package Telas;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import SYSGEST.br.UndSaude.UndSaude;
import SYSGEST.br.UndSaude.UndSaudeDAO;
import SYSGEST.br.UndSaude.UndSaudeModel;
import Telas.TelaUndSaude;
import javax.swing.JOptionPane;

/**
 *
 * @author Aelson
 */
public class TelaBuscarUndSaude extends javax.swing.JDialog {

    /**
     * Creates new form TelaBuscarUndSaude
     */
    public TelaBuscarUndSaude() {
        initComponents();
        setLocationRelativeTo(null);
        setModal(true);

        UndSaudeDAO DAO = new UndSaudeDAO();
        UndSaudeModel unS = new UndSaudeModel(DAO.listar());
        tbpesquisarESF.setModel(unS);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tfTipo = new javax.swing.JComboBox();
        edtPesqEsf = new javax.swing.JTextField();
        btBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbpesquisarESF = new javax.swing.JTable();
        btNovo = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        btExcluir = new javax.swing.JButton();
        btVoltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Manutenção Unidade de Saúde");
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel2.setText("Pesquisar.:");

        tfTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "Código ESF" }));

        btBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/buscar_1.png"))); // NOI18N
        btBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBuscarActionPerformed(evt);
            }
        });

        tbpesquisarESF.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Código ESF", "Nome da Unidade de Saúde", "Endereço"
            }
        ));
        tbpesquisarESF.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tbpesquisarESF.setSelectionBackground(new java.awt.Color(255, 255, 153));
        tbpesquisarESF.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPane1.setViewportView(tbpesquisarESF);

        btNovo.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/novo.png"))); // NOI18N
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btEditar.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/editar.png"))); // NOI18N
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/barra-pesqiusar-unnidade--de-saúde.png"))); // NOI18N

        btExcluir.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/excluir.png"))); // NOI18N
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        btVoltar.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SYSGEST/br/imagens/voltar.png"))); // NOI18N
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(tfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(edtPesqEsf)
                .addGap(18, 18, 18)
                .addComponent(btBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btEditar, btExcluir, btNovo, btVoltar});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(tfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(edtPesqEsf, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 19, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        //BOTAO NOVO TELA PESQUISAR

        TelaUndSaude tunS = new TelaUndSaude();
        dispose();
        tunS.setVisible(true);


    }//GEN-LAST:event_btNovoActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        // BOTAO EDITAR UNIDADE DE SAUDE
        int linha = tbpesquisarESF.getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(rootPane,
                    "SELECIONE UM REGISTRO PARA SER EDITADO!");
        } else {
            UndSaude undS = new UndSaude();
            int codpsf = (int) tbpesquisarESF.getValueAt(linha, 0);

            UndSaudeDAO dao = new UndSaudeDAO();
            undS = dao.pesquisacodigo(codpsf);

            TelaUndSaude tunS = new TelaUndSaude(undS);
            dispose();
            tunS.setVisible(true);

        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
        // BOTAO VOLTA PRA TELA CADASTRO ESF
        TelaUndSaude tl = new TelaUndSaude();
        dispose();
        tl.setVisible(true);

    }//GEN-LAST:event_btVoltarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        // BOTAO EXCLUIR  UNDSAUDE

        int linha = tbpesquisarESF.getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(rootPane,
                    "SELECIONE UM REGISTRO PARA SER EXCLUIDO!");
        } else {
            if (JOptionPane.showConfirmDialog(rootPane,
                    "Deseja excluir o ESF selecionado?",
                    "Excluir?", JOptionPane.YES_NO_OPTION)
                    == JOptionPane.YES_OPTION) {
                UndSaude undsaude = new UndSaude();
                int codigo = (int) tbpesquisarESF.getValueAt(linha, 0);

                UndSaudeDAO dao = new UndSaudeDAO();
                undsaude = dao.pesquisacodigo(codigo);

                dao.excluir(undsaude);
            }
            UndSaudeDAO dao = new UndSaudeDAO();
            UndSaudeModel ltm = new UndSaudeModel(dao.listar());
            tbpesquisarESF.setModel(ltm);
        }
    }

    private void btBuscarFornecedorMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TelaBuscarUndSaude().setVisible(true);
            }
        });
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBuscarActionPerformed
        // BOTAO PESQUISAR UNIDADE DE SAUDE
        if (tfTipo.getSelectedItem() == "Nome") {
            UndSaudeDAO dao = new UndSaudeDAO();
            UndSaudeModel atm = new UndSaudeModel(dao.pesquisarNome(edtPesqEsf.getText()));
            tbpesquisarESF.setModel(atm);
        } else {
            if (tfTipo.getSelectedItem() == "Código ESF") {
                UndSaudeDAO dao = new UndSaudeDAO();
                UndSaudeModel atm = new UndSaudeModel(dao.pesquisarCodigoESF(edtPesqEsf.getText()));
                tbpesquisarESF.setModel(atm);
            }
        }


    }//GEN-LAST:event_btBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBuscar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btVoltar;
    private javax.swing.JTextField edtPesqEsf;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbpesquisarESF;
    private javax.swing.JComboBox tfTipo;
    // End of variables declaration//GEN-END:variables
}
